package com.net.module.service.impl;

import com.net.module.entity.Inventory;
import com.net.module.mapper.InventoryMapper;
import com.net.module.service.IInventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author 猿仁
 * @since 2023-03-02
 */
@Slf4j
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 库存减一
     *
     * @param id 库存id
     * @return boolean
     * @author tianyu.Ge
     * @date 2023/3/2 17:00
     */
    @Transactional
    @Override
    public boolean inventorySubOne(Long id) {
        Inventory one = lambdaQuery().eq(Inventory::getId, id).one();
        if (null == one) {
            log.error("没有库存id为：" + id + "的数据！");
            return false;
        }
        Long number = one.getNumber();
        if (number == 0) {
            log.error("当前库存为0，请检查订单对应的商品正确性，期望下架，支付金额返回支付账户！");
            return false;
        }
        boolean update = lambdaUpdate().set(Inventory::getNumber, number - 1).eq(Inventory::getId, id).update();
        return update;
    }

    /**
     * 根据库存名称查询库存信息
     *
     * @param bussName
     * @return com.net.module.entity.Inventory
     * @author tianyu.Ge
     * @date 2023/3/3 8:33
     */
    @Override
    public Inventory getByBussName(String bussName) {
        return inventoryMapper.getByBussName(bussName);
    }
}
